Passed
Push — master ( 0543ef...cf6673 )
by Miloš
02:59
created

BaseAuthService.updateDbDataByFirebaseUser   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 17
rs 9.65
c 0
b 0
f 0
cc 2
1
import { Injectable } from '@angular/core';
2
import { Router } from '@angular/router';
3
import { Platform } from '@ionic/angular';
4
import { auth } from 'firebase';
5
import { Observable } from 'rxjs';
6
import { UserModel } from '../model/user-model';
7
import { AuthProvider } from '../providers/auth-provider';
8
import { IAuthProvider } from '../providers/i-auth-provider';
9
import { StorageProvider } from '../storage/storage-provider';
10
import { IAuthOptions } from './i-auth-options';
11
import { IAuthProviderOptions } from './i-auth-provider-options';
12
import { IAuthService } from './i-auth-service';
13
14
@Injectable({
15
    providedIn: 'root',
16
})
17
export class BaseAuthService<User extends UserModel = UserModel>
18
    implements IAuthService {
19
    public options: IAuthOptions = {
20
        afterLoginPage: '/',
21
        loginPage: '/login',
22
        storage: false,
23
        storageUserTable: 'users',
24
    };
25
    public providerOptions: IAuthProviderOptions = {
26
        google: {
27
            offline: true,
28
            scopes: 'profile email',
29
            webClientId: 'xxxxxx.apps.googleusercontent.com',
30
            signInType: 'popup'
31
        },
32
    };
33
34
    public constructor(
35
        protected router: Router,
36
        protected platform: Platform,
37
        protected authProvider: AuthProvider,
38
        protected storageProvider: StorageProvider<User>,
39
    ) {
40
        this.storageProvider.options.storage = this.options.storage;
41
        this.storageProvider.options.userTable = this.options.storageUserTable;
42
    }
43
44
    public async signInByProvider(
45
        provider: IAuthProvider,
46
        storeInDb = false,
47
    ): Promise<auth.UserCredential | null> {
48
        const credential = await provider.handleLogin();
49
        if (storeInDb && credential && credential.user !== null) {
50
            await this.storageProvider
51
                .getProvider()
52
                .updateStoredDataByFirebaseUser(credential.user);
53
        }
54
        this.onAfterLogin();
55
        return credential;
56
    }
57
58
    public async signInAnonymously(storeInDb = false): Promise<any> {
59
        return this.signInByProvider(this.authProvider.authAnonymous);
60
    }
61
62
    public async signInViaEmail(storeInDb = false): Promise<any> {
63
        return this.signInByProvider(this.authProvider.authEmail);
64
    }
65
66
    public async signInViaFacebook(storeInDb = false): Promise<any> {
67
        return this.signInByProvider(this.authProvider.authFacebook);
68
    }
69
70
    public async signInViaGithub(storeInDb = false): Promise<any> {
71
        return this.signInByProvider(this.authProvider.authGithub);
72
    }
73
74
    public async signInViaGoogle(storeInDb = false): Promise<any> {
75
        return this.signInByProvider(this.authProvider.authGoogle);
76
    }
77
78
    public async signInViaPhone(storeInDb = false): Promise<any> {
79
        return this.signInByProvider(this.authProvider.authPhone);
80
    }
81
82
    public async signInViaTwitter(storeInDb = false): Promise<any> {
83
        return this.signInByProvider(this.authProvider.authTwitter);
84
    }
85
86
    /**
87
     * Handle sign out request
88
     */
89
    public async signOut(): Promise<void> {
90
        this.authProvider;
91
        this.onAfterSignOut();
92
    }
93
94
    /**
95
     * Get user profile data
96
     */
97
    public getUser(): Observable<unknown | User | null> {
98
        return this.storageProvider.getUser();
99
    }
100
101
    protected onAfterSignOut() {
102
        if (this.options.loginPage) {
103
            this.router.navigate([this.options.loginPage]);
104
        }
105
    }
106
107
    protected onAfterLogin() {
108
        if (this.options.afterLoginPage) {
109
            this.router.navigate([this.options.afterLoginPage]);
110
        }
111
    }
112
}
113